﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcFinalTest.Data;
using MvcFinalTest.Models;
using System.Collections.ObjectModel;

namespace MvcFinalTest.Controllers
{
    public class EmployeeController : Controller
    {
        //
        // GET: /Employee/

        public ActionResult Index()
        {
            Collection<EmployeeModel> employee = EmployeeRepository.GetAll();
            ViewData["EmployeeData"] = employee.Count().ToString();
            return View(employee);
        }

        //
        // GET: /Employee/Details/5

        public ActionResult Details(int id)
        {
            EmployeeModel employee = EmployeeRepository.GetEmployeeById(id);
            return View(employee);
        }

        //
        // GET: /Employee/Create

        public ActionResult Create()
        {
            List<SelectListItem> itemsBranch = new List<SelectListItem>();
            List<SelectListItem> itemsJobType = new List<SelectListItem>();
            Collection<BranchModel> branch = BranchRepository.GetAll();
            Collection<JobTypeModel> jobType = JobTypeRepository.GetAll();
            itemsBranch.Add(new SelectListItem
            {
                Text = "Select One ...",
                Value = "",
                Selected = true
            });
            foreach (var item in branch)
            {
                itemsBranch.Add(new SelectListItem
                {
                    Text = item.Address + ", " + item.Name,
                    Value = item.BranchId.ToString()
                });
            }
            itemsJobType.Add(new SelectListItem
            {
                Text = "Select One ...",
                Value = "",
                Selected = true
            });
            foreach (var item in jobType)
            {
                itemsJobType.Add(new SelectListItem
                {
                    Text = item.Name,
                    Value = item.JobTypeId.ToString()
                });
            }

            ViewData["BranchId"] = itemsBranch;
            ViewData["JobTypeId"] = itemsJobType;
           
            return View();
        } 

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            EmployeeModel employee = new EmployeeModel();
            //EmployeeRepository er = new EmployeeRepository();
            List<SelectListItem> itemsBranch = new List<SelectListItem>();
            List<SelectListItem> itemsJobType = new List<SelectListItem>();
            Collection<BranchModel> branch = BranchRepository.GetAll();
            Collection<JobTypeModel> jobType = JobTypeRepository.GetAll();
            itemsBranch.Add(new SelectListItem
            {
                Text = "Select One ...",
                Value = "",
                Selected = true
            });
            foreach (var item in branch)
            {
                itemsBranch.Add(new SelectListItem
                {
                    Text = item.Address + ", " + item.Name,
                    Value = item.BranchId.ToString()
                });
            }
            itemsJobType.Add(new SelectListItem
            {
                Text = "Select One ...",
                Value = "",
                Selected = true
            });
            foreach (var item in jobType)
            {
                itemsJobType.Add(new SelectListItem
                {
                    Text = item.Name,
                    Value = item.JobTypeId.ToString()
                });
            }
            try
            {
                if ((collection["FirstName"].Length + collection["LastName"].Length) > 100)
                {
                    ModelState.AddModelError("FirstName", "Sum of First Name and Last Name Can Not Exceed 100 Characters");
                    ModelState.AddModelError("LastName", "Sum of First Name and Last Name Can Not Exceed 100 Characters");
                }
                else
                {
                    if (TryUpdateModel(employee))
                    {
                        if (ModelState.IsValid)
                        {
                            EmployeeRepository.Add(employee);
                            return RedirectToAction("Index");
                        }
                    }   
                }                      
            }
            catch
            {                
                ViewData["BranchId"] = itemsBranch;
                ViewData["JobTypeId"] = itemsJobType;
                return View(employee);
            }
            ViewData["BranchId"] = itemsBranch;
            ViewData["JobTypeId"] = itemsJobType;
            return View(employee);
        }
        
        //
        // GET: /Employee/Edit/5
 
        public ActionResult Edit(int id)
        {
            EmployeeModel employee = EmployeeRepository.GetEmployeeById(id);
            List<SelectListItem> itemsBranch = new List<SelectListItem>();
            List<SelectListItem> itemsJobType = new List<SelectListItem>();
            Collection<BranchModel> branch = BranchRepository.GetAll();
            Collection<JobTypeModel> jobType = JobTypeRepository.GetAll();
            itemsBranch.Add(new SelectListItem
            {
                Text = "Select One ...",
                Value = ""
            });
            foreach (var item in branch)
            {
                itemsBranch.Add(new SelectListItem
                {
                    Text = item.Address + ", " + item.Name,
                    Value = item.BranchId.ToString()
                });
            }
            itemsJobType.Add(new SelectListItem
            {
                Text = "Select One ...",
                Value = ""
            });
            foreach (var item in jobType)
            {
                itemsJobType.Add(new SelectListItem
                {
                    Text = item.Name,
                    Value = item.JobTypeId.ToString()
                });
            }

            SelectListItem jobtypeItem = itemsJobType.Where(j => j.Value.Equals(employee.JobTypeId.ToString())).SingleOrDefault();
            jobtypeItem.Selected = true;
            SelectListItem branchItem = itemsBranch.Where(b => b.Value.Equals(employee.BranchId.ToString())).SingleOrDefault();
            branchItem.Selected = true;
            ViewData["BranchId"] = itemsBranch;
            ViewData["JobTypeId"] = itemsJobType;
            return View(employee);
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            EmployeeModel employee = EmployeeRepository.GetEmployeeById(id);
            try
            {
                if ((collection["FirstName"].Length + collection["LastName"].Length) > 100)
                {
                    ModelState.AddModelError("FirstName", "Sum of First Name and Last Name Can Not Exceed 100 Characters");
                    ModelState.AddModelError("LastName", "Sum of First Name and Last Name Can Not Exceed 100 Characters");
                }
                else
                {
                    if (TryUpdateModel(employee))
                    {
                        EmployeeRepository.Edit(employee);
                        return RedirectToAction("Index");
                    }
                }          
            }
            catch
            {
                return View(employee);
            }
            return View(employee);
        }

        //
        // GET: /Employee/Delete/5
 
        public ActionResult Delete(int id)
        {
            EmployeeRepository.Delete(id);
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Search (FormCollection collection)
        {
            List<EmployeeModel> employee = EmployeeRepository.Search(collection["FirstName"]);
            ViewData["EmployeeData"] = employee.Count().ToString();
            return View("Index", employee);
        }
    }
}
